package com.triones.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.triones.api.entity.BankCardDO;
import com.triones.api.entity.CryptoChainDO;
import com.triones.api.entity.CryptoCurrencyDO;
import com.triones.api.entity.sta.ChainTotal;
import com.triones.api.entity.sta.CryptoMemberWithdrawProfit;
import com.triones.api.entity.sta.CryptoSystemWithdrawProfit;
import com.triones.api.entity.sta.ValueName;
import com.triones.api.mapper.CryptoBillDetailMapper;
import com.triones.api.mapper.CryptoBillMapper;
import com.triones.api.mapper.CryptoChainMapper;
import com.triones.api.mapper.CryptoCurrencyMapper;
import com.triones.api.service.FinanceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 财务账单 -业务实现
 */
@Service
@RequiredArgsConstructor
public class FinanceServiceImpl implements FinanceService {

    private final CryptoBillMapper cryptoBillMapper;

    private final CryptoChainMapper cryptoChainMapper;

    private final CryptoCurrencyMapper cryptoCurrencyMapper;

    private final CryptoBillDetailMapper cryptoBillDetailMapper;

    @Override
    public Map cryptoDepositTotalSta() {
        Map result = new HashMap();
        List<String> scenes = Arrays.asList("transfer-in");
        List<String> chainStrList = new ArrayList();
        LambdaQueryWrapper<CryptoCurrencyDO> currencyLqw = new LambdaQueryWrapper<CryptoCurrencyDO>();
        currencyLqw.eq(CryptoCurrencyDO::getStatus, "0")
            .eq(CryptoCurrencyDO::getDel, "0");
        List<CryptoCurrencyDO> currencyList = cryptoCurrencyMapper.selectList(currencyLqw);
        if (currencyList.size() > 0) {
            List<String> ccyList = currencyList.stream().map(CryptoCurrencyDO::getCcy).collect(Collectors.toSet()).stream().collect(Collectors.toList());
            result.put("ccyList", ccyList);
            List<List<String>> ccyTotalList = new ArrayList();
            for (String str : ccyList) {
                List<ChainTotal> chainTotalList = cryptoBillMapper.selectCcyTotalByChain(str, scenes);
                if (chainTotalList.size() > 0) {
                    chainStrList = chainTotalList.stream()
                        .map(ChainTotal::getChain)
                        .collect(Collectors.toList());
                    ccyTotalList.add(chainTotalList.stream()
                        .map(ChainTotal::getTotal)
                        .collect(Collectors.toList()));
                }
            }
            result.put("chainStrList", chainStrList);
            result.put("ccyTotalList", ccyTotalList);
        }
        return result;
    }

    @Override
    public Map cryptoPoolTotalSta() {
        return null;
    }

    @Override
    public Map cryptoWithdrawTotalSta() {
        Map result = new HashMap();
        List<String> scenes = Arrays.asList("transfer-out", "transfer-fiat");
        List<String> chainStrList = new ArrayList();
        LambdaQueryWrapper<CryptoCurrencyDO> currencyLqw = new LambdaQueryWrapper<CryptoCurrencyDO>();
        currencyLqw.eq(CryptoCurrencyDO::getStatus, "0")
            .eq(CryptoCurrencyDO::getDel, "0");
        List<CryptoCurrencyDO> currencyList = cryptoCurrencyMapper.selectList(currencyLqw);
        if (currencyList.size() > 0) {
            List<String> ccyList = currencyList.stream().map(CryptoCurrencyDO::getCcy).collect(Collectors.toSet()).stream().collect(Collectors.toList());
            result.put("ccyList", ccyList);
            List<List<String>> ccyTotalList = new ArrayList();
            for (String str : ccyList) {
                List<ChainTotal> chainTotalList = cryptoBillMapper.selectCcyTotalByChain(str, scenes);
                if (chainTotalList.size() > 0) {
                    chainStrList = chainTotalList.stream()
                        .map(ChainTotal::getChain)
                        .collect(Collectors.toList());
                    ccyTotalList.add(chainTotalList.stream()
                        .map(ChainTotal::getTotal)
                        .collect(Collectors.toList()));
                }
            }
            result.put("chainStrList", chainStrList);
            result.put("ccyTotalList", ccyTotalList);
        }
        return result;
    }

    @Override
    public Map cryptoMemberWithdrawProfitSta(String scene) {
        Map result = new HashMap();
        if ("transfer-out".equals(scene)) {
            List<CryptoMemberWithdrawProfit> list = cryptoBillMapper.cryptoMemberWithdrawProfitStaByTransferOut();
            result.put("list", list);
        }
        if ("transfer-fiat".equals(scene)) {
            List<CryptoMemberWithdrawProfit> list = cryptoBillMapper.cryptoMemberWithdrawProfitStaByTransferFiat();
            result.put("list", list);
        }
        return result;
    }

    @Override
    public Map cryptoSystemWithdrawProfitSta(String scene) {
        Map result = new HashMap();
        if ("transfer-out".equals(scene)) {
            List<CryptoSystemWithdrawProfit> list = cryptoBillDetailMapper.cryptoSystemWithdrawOutProfitSta();
            result.put("list", list);
        }
        if ("transfer-fiat".equals(scene)) {
            List<CryptoSystemWithdrawProfit> list = cryptoBillDetailMapper.cryptoSystemWithdrawFiatProfitSta();
            result.put("list", list);
        }
        return result;
    }

    @Override
    public Map cryptoDepositTotalByChainAndYearSta(String chain, String year) {
        Map result = new HashMap();
        List<ValueName> list = cryptoBillMapper.cryptoDepositTotalByChainAndYearSta(chain, year);
        if (list.size() > 0) {
            List<String> legendList = list.stream().map(ValueName::getName).collect(Collectors.toSet()).stream().collect(Collectors.toList());
            result.put("legendList", legendList);
        }
        result.put("list", list);
        return result;
    }
}
